package com.aistar.controller;

import com.aistar.mapper.AncommMapper;
import com.aistar.mapper.MemberMapper;
import com.aistar.pojo.*;
import com.aistar.service.MemberService;
import com.aistar.service.myfoot.*;
import com.aistar.service.reply.ReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author shkstart
 * @date 2019/10/1 - 21:16
 */

@Controller
public class MyfootController implements Serializable {

    @RequestMapping("myfoot/mapview/{userId}")
    public ModelAndView myMap(@PathVariable("userId") int userId,HttpServletRequest request){
        ModelAndView mov = new ModelAndView();

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        System.out.println("ss:"+ss);
        int userIddd = Integer.parseInt(String.valueOf(ss));
        Member member = memberService.getbyId(userIddd);

        mov.addObject("memberall",member);

        mov.setViewName("MyFoot/myfoot2");
        mov.addObject("map",userId);
        return mov;
    }
    @Autowired
    private MyfootService myfootService;
    @Autowired
    private SelectCityByUserIdService selectCityByUserIdService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private MemberService memberService;
    @ResponseBody
    @PostMapping("myfoot/setcity")
    //    1.添加城市
    public int addMyfoot(String add_city,HttpServletRequest request){
        Date date = new Date();
        System.out.println("controller走到");
        System.out.println(selectProvinceByCity(add_city));
        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        System.out.println("userid" + ss);
        int userId = Integer.parseInt(String.valueOf(ss));
        return myfootService.setMyfoot(selectProvinceByCity(add_city),add_city,date,userId);
    }
    //    1.添加城市
    //利用字符串类的一个方法，indexof();即判断一个字符串是否包含另一个字符串；如果是，则返回出现的位置；如果不是，则返回-1
    public String selectProvinceByCity(String city){
        System.out.println("selectProvinceByCity走到");
        Map<String, String> map = new HashMap<String, String>();
        System.out.println("创建了Map<String, String> map = new HashMap<String, String>();");
        map.put("新疆","乌鲁木齐、克拉玛依、石河子、阿拉尔市、图木舒克、五家渠、哈密、吐鲁番、阿克苏、喀什、和田、伊宁、塔城、阿勒泰、奎屯、博乐、昌吉、阜康、库尔勒、阿图什、乌苏");
        map.put("宁夏","银川、石嘴山、吴忠、固原、中卫、青铜峡市、灵武市");
        map.put("内蒙古","呼和浩特、包头、乌海、赤峰、通辽、鄂尔多斯、呼伦贝尔、巴彦淖尔、乌兰察布11县级市——霍林郭勒市、满洲里市、牙克石市、扎兰屯市、根河市、额尔古纳市、丰镇市、锡林浩特市、二连浩特市、乌兰浩特市、阿尔山市");
        map.put("黑龙江","南宁、柳州、桂林、梧州、北海、崇左、来宾、贺州、玉林、百色、河池、钦州、防城港、贵港、五常、双城、尚志、纳河、虎林、密山、铁力、同江、富锦、绥芬河、海林、宁安、穆林、北安、五大连池、肇东、海伦、安达");
        map.put("吉林","长春、吉林、四平、辽源、通化、白山、松原、白城、九台市、榆树市、德惠市、舒兰市、桦甸市、蛟河市、磐石市、公主岭市、双辽市、梅河口市、集安市、临江市、大安市、洮南市、延吉市、图们市、敦化市、龙井市、珲春市、和龙市");
        map.put("辽宁","沈阳、大连、鞍山、抚顺、本溪、丹东、锦州、营口、阜新、辽阳、盘锦、铁岭、朝阳、葫芦岛、新民、瓦房店、普兰、庄河、海城、东港、凤城、凌海、北镇、大石桥、盖州、灯塔、调兵山、开原、凌源、北票、兴城");
        map.put("河北", "石家庄、唐山、邯郸、秦皇岛、保定、张家口、承德、廊坊、沧州、衡水、邢台、辛集市、藁城市、晋州市、新乐市、鹿泉市、遵化市、迁安市、武安市、南宫市、沙河市、涿州市、定州市、安国市、高碑店市、泊头市、任丘市、黄骅市、河间市、霸州市、三河市、冀州市、深州市");
        map.put("山东","济南、青岛、淄博、枣庄、东营、烟台、潍坊、济宁、泰安、威海、日照、莱芜、临沂、德州、聊城、菏泽、滨州、章丘、胶南、胶州、平度、莱西、即墨、滕州、龙口、莱阳、莱州、招远、蓬莱、栖霞、海阳、青州、诸城、安丘、高密、昌邑、兖州、曲阜、邹城、乳山、文登、荣成、乐陵、临清、禹城");
        map.put("江苏","南京、镇江、常州、无锡、苏州、徐州、连云港、淮安、盐城、扬州、泰州、南通、宿迁、江阴市、宜兴市、邳州市、新沂市、金坛市、溧阳市、常熟市、张家港市、太仓市、昆山市、吴江市、如皋市、通州市、海门市、启东市、东台市、大丰市、高邮市、江都市、仪征市、丹阳市、扬中市、句容市、泰兴市、姜堰市、靖江市、兴化市");
        map.put("安徽","合肥、蚌埠、芜湖、淮南、亳州、阜阳、淮北、宿州、滁州、安庆、巢湖、马鞍山、宣城、黄山、池州、铜陵、界首、天长、明光、桐城、宁国");
        map.put("浙江","杭州、嘉兴、湖州、宁波、金华、温州、丽水、绍兴、衢州、舟山、台州、建德市、富阳市、临安市、余姚市、慈溪市、奉化市、瑞安市、乐清市、海宁市、平湖市、桐乡市、诸暨市、上虞市、嵊州市、兰溪市、义乌市、东阳市、永康市、江山市、临海市、温岭市、龙泉市");
        map.put("福建","福州、厦门、泉州、三明、南平、漳州、莆田、宁德、龙岩、福清市、长乐市、永安市、石狮市、晋江市、南安市、龙海市、邵武市、武夷山、建瓯市、建阳市、漳平市、福安市、福鼎市");
        map.put("广东","广州、深圳、汕头、惠州、珠海、揭阳、佛山、河源、阳江、茂名、湛江、梅州、肇庆、韶关、潮州、东莞、中山、清远、江门、汕尾、云浮");
        map.put("云南","昆明、曲靖、玉溪、保山、昭通、丽江、普洱、临沧");
        map.put("贵州", "贵阳、六盘水、遵义、安顺");
        map.put("四川","成都、绵阳、德阳、广元、自贡、攀枝花、乐山、南充、内江、遂宁、广安、泸州、达州、眉山、宜宾、雅安、资阳");
        map.put("湖南","长沙、株洲、湘潭、衡阳、岳阳、郴州、永州、邵阳、怀化、常德、益阳、张家界、娄底");
        map.put("湖北","武汉、襄樊、宜昌、黄石、鄂州、随州、荆州、荆门、十堰、孝感、黄冈、咸宁");
        map.put("河南","郑州、洛阳、开封、漯河、安阳、新乡、周口、三门峡、焦作、平顶山、信阳、南阳、鹤壁、濮阳、许昌、商丘、驻马店、巩义市、新郑市、新密市、登封市、荥阳市、偃师市、汝州市、舞钢市、林州市、卫辉市、辉县市、沁阳市、孟州市、禹州市、长葛市、义马市、灵宝市、邓州市、永城市、项城市、济源市");
        map.put("山西","太原、大同、忻州、阳泉、长治、晋城、朔州、晋中、运城、临汾、吕梁");
        map.put("陕西","西安、咸阳、铜川、延安、宝鸡、渭南、汉中、安康、商洛、榆林");
        map.put("甘肃", "兰州、天水、平凉、酒泉、嘉峪关、金昌、白银、武威、张掖、庆阳、定西、陇南");
        map.put("江西", "南昌、九江、赣州、吉安、鹰潭、上饶、萍乡、景德镇、新余、宜春、抚州");
        System.out.println("map.put()插入完成");
//        String province = null;
//        for(String key: map.keySet()){
//            System.out.println("key:"+key);
//            if(map.get(key).contains(city)){
//                province = key;
//                System.out.println("province:"+province);
//                break;
//            }
//        }
//        return province;

        Set<Map.Entry<String, String>> proviceSet = map.entrySet();
        String province = null;
        Iterator<Map.Entry<String, String>> iterator = proviceSet.iterator();
        System.out.println("迭代器对象创建完成");
        while (iterator.hasNext()){
            System.out.println(iterator.hasNext());
            Map.Entry<String,String> m = iterator.next();
            System.out.println("province:"+m.getValue());
            if(m.getValue().contains(city)){
                province = m.getKey();
                System.out.println("province:"+province);
                break;
            }

        }
        return province;
    }

    //    2.根据userid查询城市

    @PostMapping("myfoot/selectcity")
    @ResponseBody
    public List<Footprint> selectCityByUserId(int id){
        System.out.println(selectCityByUserIdService.selectCityByUserId(id));
        return selectCityByUserIdService.selectCityByUserId(id);
    }

    @Autowired
    private MyTravelService myTravelService;
    @PostMapping("myfoot/count")
    @ResponseBody
    public String countOfNotes(int userid){
        List list= replyService.reply(userid);
        Iterator iterator = list.iterator();
        int sum = 0;
        while (iterator.hasNext()){
            Map m = (Map) iterator.next();
            Set set = m.keySet();
            for (Object ele:set) {
               if( ele.getClass().getName().contains("Tracomm")){
                    Tracomm tracomm = (Tracomm) ele;
                   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                   String t1 = sdf.format(tracomm.getTracTime());
                   String t2 = sdf.format(new Date());
                   System.out.println("t1:"+t1);
                   System.out.println("t2:"+t2);
                   if(t1.equals(t2)){
                     sum += 1;
                   }
               }
                if( ele.getClass().getName().contains("Answer")){
                    Answer answer = (Answer) ele;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String t1 = sdf.format(answer.getAnsTime());
                    String t2 = sdf.format(new Date());
                    if(t1.equals(t2)){
                        sum += 1;
                    }
                }
                if( ele.getClass().getName().contains("Ancomm")){
                    Ancomm ancomm = (Ancomm) ele;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String t1 = sdf.format(ancomm.getAncoTime());
                    String t2 = sdf.format(new Date());
                    if(t1.equals(t2)){
                        sum += 1;
                    }
                }
            }
        }
        System.out.println("sum:"+sum);
        return myTravelService.countOfNote(userid)+","+myTravelService.countVisited(userid)+","+list.size()+","+sum;
    }

    @Autowired
    private CollectionService collectionService;
    @Autowired
    private MemberMapper memberMapper;
    @GetMapping("myfoot/collect/{userid}")
    public ModelAndView collection(@PathVariable("userid") int userid,HttpServletRequest request){
        List<TravCollect> list= collectionService.getCollection(userid);
        System.out.println("list:"+list);
        Iterator<TravCollect> iterator = list.iterator();
        List<CollectionVO> list1 = new ArrayList<>();
        while (iterator.hasNext()){
            CollectionVO cvo = new CollectionVO();
            TravCollect travCollect = iterator.next();
            Travelnote travelnote = myTravelService.selectNoteById(travCollect.getCollectTravId());
            System.out.println("travCollect.getCollectTravId():"+travCollect.getCollectTravId()+"\n"+"travelnote:"+travelnote);
            //                    发表游记用户id
            cvo.setTravWid(travelnote.getTravWid());
            //                    发表游记的用户
            cvo.setMemeber(memberMapper.selectByPrimaryKey(travelnote.getTravWid()));
            //                    游记标题
            cvo.setTravTitle(travelnote.getTravTitle());
            //                    游记内容
            cvo.setTravText(travelnote.getTravOther1());
            //                    游记浏览数
            cvo.setTravLooknumber(travelnote.getTravLooknumber());
            //                    游记点赞数
            cvo.setTravPraise(travelnote.getTravPraise());
            //                    游记景色图片
            cvo.setTravPic(travelnote.getTravPic());
            //                    发表地点
            cvo.setLocation(travelnote.getTravLoction());
            //游记id
            cvo.setTravelId(travelnote.getTravId());
            list1.add(cvo);
        }



        PageHelper.startPage(0,2);
        PageInfo<CollectionVO> pageInfo = new PageInfo<>(list1);

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member member = memberService.getbyId(userId);

        ModelAndView mav = new ModelAndView();
        mav.addObject("memberall",member);
        mav.setViewName("MyFoot/Mycollection");
        mav.addObject("list",pageInfo.getList());
        System.out.println("pageInfo.getList().size():"+pageInfo.getList().size());
        return mav;
    }
    @Autowired
    private MyAnswerService myAnswerService;
    @Autowired
    private MyQuestionService myQuestionService;
    @GetMapping("/myfoot/answer/{userId}")
//    @ResponseBody
    public ModelAndView answer(@PathVariable("userId") int id, HttpServletRequest request){
        System.out.println("userId:"+id);
        List<Answer> list1 = myAnswerService.selectAnsByUserId(id);
        List<CollectionVO> list2 = new ArrayList<>();
        System.out.println("list1:"+list1);
        Iterator<Answer> iterator = list1.iterator();
        while (iterator.hasNext()){
//            本次调用了VO类：CollectionVO，原为收藏表而设置，但在此处依然可用
            CollectionVO collectionVO = new CollectionVO();
            Answer answer = iterator.next();

//        问表：对应的的问题标题
            Question question = myQuestionService.selectQueById(answer.getAnsQueId());
            collectionVO.setTravTitle(question.getQueTitle());
//        答表：用户的回答内容
            collectionVO.setTravText(answer.getAnsText());
//        答表：点赞数
            collectionVO.setTravPraise(answer.getAnsPraise());
//        用户表：发表地点
            collectionVO.setLocation(question.getQueLoction());
//        用户表：发表用户
            collectionVO.setMemeber(memberMapper.selectByPrimaryKey(question.getQueUserId()));
//        问表：浏览数
            collectionVO.setTravLooknumber(question.getQueAnnumber());
            list2.add(collectionVO);
//         问表：问题配图
            collectionVO.setTravPic(question.getQueOther1());
            //问表id
           collectionVO.setTravelId(question.getQueId());
        }

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member member = memberService.getbyId(userId);



        ModelAndView mov = new ModelAndView();
        mov.addObject("memberall",member);
        mov.setViewName("MyFoot/myqa");
        mov.addObject("list1",list2);
        System.out.println("list2:"+list2);
        return mov;
    }
    @PostMapping("/myfoot/member")
    @ResponseBody
    public Member memberResult(int userId){
        return memberMapper.selectByPrimaryKey(userId);
    }

    @Autowired
    private AncommMapper ancommMapper;
    @RequestMapping("myfoot/otheranswer/{id}")
    public ModelAndView otherAnswer(@PathVariable("id") int id,HttpServletRequest request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm");
        System.out.println("id:"+id);
        List<Answer> list1 = myAnswerService.selectAnsByUserId(id);
        List<CollectionVO> list2 = new ArrayList<>();
        System.out.println("list1:"+list1);
        Iterator<Answer> iterator = list1.iterator();
        while (iterator.hasNext()){
//            本次调用了VO类：CollectionVO，原为收藏表而设置，但在此处依然可用
            CollectionVO collectionVO = new CollectionVO();
            Answer answer = iterator.next();
//        问表：对应的的问题标题
            Question question = myQuestionService.selectQueById(answer.getAnsQueId());
            collectionVO.setTravTitle(question.getQueTitle());
//        答表：用户的回答内容
            collectionVO.setTravText(answer.getAnsText());
//        答表：点赞数
            collectionVO.setTravPraise(answer.getAnsPraise());
//        问表：发表地点
            collectionVO.setLocation(question.getQueLoction());
//        用户表：发表用户
            collectionVO.setMemeber(memberMapper.selectByPrimaryKey(question.getQueUserId()));
//        评论表：浏览数或着评论量
            AncommExample ancommExample = new AncommExample();
            AncommExample.Criteria criteria = ancommExample.createCriteria();
            criteria.andAncoTableIdEqualTo(answer.getAnsId());
            int num = ancommMapper.selectByExampleWithBLOBs(ancommExample).size();
            collectionVO.setTravLooknumber(num);
//            答表：点赞量：
            collectionVO.setTravPraise(answer.getAnsPraise());
//        问表：问题配图
            collectionVO.setTravPic(question.getQueOther1());
//        答表：发布时间
            System.out.println(sdf.format(answer.getAnsTime()));
            collectionVO.setDay(answer.getAnsTime());
            list2.add(collectionVO);
        }

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member member = memberService.getbyId(userId);


        ModelAndView mov = new ModelAndView();
        mov.addObject("aaa",id);
        mov.addObject("memberall",member);
        mov.setViewName("HisHome/Hisqa");
        mov.addObject("list1",list2);
        System.out.println("list2:"+list2);
        return mov;
    }
}
